package com.wgh.common.vo;

import com.wgh.common.constant.Constants;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 通用返回结果
 * 
 * @author AI Shop
 */
@Data
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
    private String code;

    /**
     * 消息
     */
    private String message;

    /**
     * 数据
     */
    private T data;

    /**
     * 附加数据
     */
    private Map<String, Object> extra = new HashMap<>();

    /**
     * 成功返回结果
     */
    public static <T> Result<T> success() {
        Result<T> result = new Result<>();
        result.setCode(Constants.SUCCESS);
        result.setMessage("操作成功");
        return result;
    }

    /**
     * 成功返回结果
     */
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setCode(Constants.SUCCESS);
        result.setMessage("操作成功");
        result.setData(data);
        return result;
    }

    /**
     * 成功返回结果
     */
    public static <T> Result<T> success(String message, T data) {
        Result<T> result = new Result<>();
        result.setCode(Constants.SUCCESS);
        result.setMessage(message);
        result.setData(data);
        return result;
    }

    /**
     * 失败返回结果
     */
    public static <T> Result<T> error() {
        Result<T> result = new Result<>();
        result.setCode(Constants.FAIL);
        result.setMessage("操作失败");
        return result;
    }

    /**
     * 失败返回结果
     */
    public static <T> Result<T> error(String message) {
        Result<T> result = new Result<>();
        result.setCode(Constants.FAIL);
        result.setMessage(message);
        return result;
    }

    /**
     * 失败返回结果
     */
    public static <T> Result<T> error(String code, String message) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    /**
     * 添加附加数据
     */
    public Result<T> addExtra(String key, Object value) {
        this.extra.put(key, value);
        return this;
    }

    /**
     * 判断是否成功
     */
    public boolean isSuccess() {
        return Constants.SUCCESS.equals(this.code);
    }
}